1754A - Technical Support - CodeForces Solution


*800

Please click on ads to support us..

Python Code:

def solve():
    _ = int(input())
    t = input()
    ctn = 0
    for i in t:
        if i == 'Q':
            ctn += 1
        else:
            ctn -= 1

        if ctn < 0:
            ctn = 0
    return "Yes" if ctn == 0 else "No"


if __name__ == "__main__":
    t = int(input())
    for _ in range(t):
        print(solve())

C++ Code:

//MANISHk
#include<bits/stdc++.h>
using namespace std;
#define ll long long

#define db double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define frr(i, k, n) for (int i = n - 1; i >= k; i--)
#define fr(i, k, n) for (int i = k; i < n; i++)


long long f(int a, int b) {
if(b == 0) return 1;
long long p = f(a,b/2);
if(b%2==0) return (p*p)%1000000007;
else return (((p*p)%1000000007) * a) % 1000000007;
}


ll gcd(ll a, ll b){
if (a == 0)
return b;
if (b == 0)
return a;
if (a < b)
return gcd(a, b % a);
return gcd(b, a % b);
}

ll lcm (ll a , ll b){
return (a*b) / gcd(a , b);
}

bool isperfect(double n){
if(n >= 0){
ll x = sqrt(n);
return (x*x == n);
}
else
return false;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll modInverse(ll b, ll m){
ll x, y;
ll g = gcdExtended(b, m, &x, &y);
if (g != 1)
return -1;
return (x%m + m) % m;
}

ll modDivide(ll a, ll b, ll m){
a = a % m;
ll inv = modInverse(b, m);
return (inv * a) % m;
}

ll gcdExtended(ll a, ll b, ll *x, ll *y){
if (a == 0){
*x = 0, *y = 1;
return b;
}

ll x1;ll y1;
ll gcd = gcdExtended(b%a, a, &x1, &y1);

*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve(){
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll qc=0,ac=0;
    for(int i=0;i<n;i++){
        if(s[i]=='Q')qc++;
        else qc--;
        if(qc<0)qc=0;
    }
    if(qc!=0)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
   // cout<<qc<<ac<<endl;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
ll t;
cin>>t;
while(t--){
solve();
}
}


Comments

Submit
0 Comments
More Questions

1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy